/*
 * @Author: your name
 * @Date: 2021-11-22 08:06:36
 * @LastEditTime: 2022-04-04 11:02:50
 * @Descripttion: 描述
 */
#include "input_edit.h"
InputEdit::InputEdit(std::unique_ptr<Widget> &page, std::string &string, Widget *editView)
    : InputItem(page, string, editView, false)
{
    // UrlItem();
    // SetEditTips("sssssssss");
}
InputEdit::~InputEdit()
{
    // UrlItemExit();
}
void InputEdit::SetEditText(Widget *edit, std::string value, void *ctx)
{
    /**
     * @brief
     * 开始显示字符下标。
     */
    GetEditValue() = value;
    // RefreshPageCommon(GetString());
    std::unique_ptr<Widget> view(GuiEngine::GetInstance().NewWidget());
    if (GuiEngine::GetInstance().Lookup(edit, EDIT_VALUE, true, view.get()))
    {
        GuiEngine::GetInstance().Destroy(view.get());
    }
    printf("@@@@@SetEditText=%s\n\r", value.c_str());
    // GuiEngine::GetInstance().EditSetTrTips(edit, "");
    if (GuiEngine::GetInstance().ViewCreate(edit, 0, 0, 0, 0, view.get()))
    {
        GuiEngine::GetInstance().SetName(view.get(), EDIT_VALUE);
        GuiEngine::GetInstance().UseStyle(view.get(), "inputView");
        GuiEngine::GetInstance().SetFloating(view.get(), true);
        GuiEngine::GetInstance().SetSelfLayout(view.get(), "default(x=2,y=m,w=52%,h=96%)");
        /**
         * @brief
         * 设置项view控件有1行、1列，设置项按数量靠左排列
         */
        GuiEngine::GetInstance().SetChildrenLayout(view.get(), "default(r=1,c=0,align_h=left,x=0,y=0,s=0)");
        if (0 == value.length())
        {
            ShowEditTips(view.get());
            return;
        }
        std::unique_ptr<Widget> view2(GuiEngine::GetInstance().NewWidget());
        for (unsigned int i = GetStartIndex(); i < value.length(); i++)
        {
            if (GuiEngine::GetInstance().LabelCreate(view.get(), 0, 0, 0, 0, view2.get()))
            {
                SetEditString(view2.get(), value, i);
            }
            if (i - GetStartIndex() + 1 >= EDIT_LENGTH)
            {
                break;
            }
        }
    }
}
void InputEdit::InputFinshedEvent()
{
    if (setEditCallback)
    {
        setEditCallback();
    }
}
void InputEdit::SetEditChangedCallback(std::function<void()> func)
{
    setEditCallback = func;
}